package fei.zhou.springboot1.business.bean.demo1;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

@Configuration
@ComponentScan("fei.zhou.springboot1.business.bean.demo1")

//注入配置文件
@PropertySource("classpath:demo1/test.properties")
public class ElConfig {

    //常量
    @Value("111")
    private String normal;

    //注入操作系统属性
    @Value("#{systemProperties['os.name']}")
    private String osName;

    //注入表达式结果
    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private double randomNumber;

    //注入其他bean属性
    @Value("#{person3.lastName}")
    private String fromAnother;

    //注入文件资源
    @Value("classpath:demo1/1.txt")
    private Resource testFile;

    //注入网站资源
    @Value("http://www.baidu.com")
    private Resource testUrl;

    //注入配置文件
    @Value("${book.name}")
    private String bookName;

    @Autowired
    private Environment environment; //7

    @Bean //7
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Override
    public String toString() {
        return "ElConfig{" +
                "normal='" + normal + '\'' +
                ", osName='" + osName + '\'' +
                ", randomNumber=" + randomNumber +
                ", fromAnother='" + fromAnother + '\'' +
                ", testFile=" + testFile +
                ", testUrl=" + testUrl +
                ", bookName='" + bookName + '\'' +
                ", environment=" + environment +
                '}';
    }


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(ElConfig.class);


        ElConfig elConfig = context.getBean(ElConfig.class);

        System.out.println( "------------");

        System.out.println( elConfig);


        context.close();
    }
}
